import React, { Component } from 'react';
import moment from 'moment';

export default class TimeUtils extends React.Component
{
    static isBeforeCurrentDate(value, tmUserLen)
    {
        var selectDate = TimeUtils.parserDateString(value + ':00');
        var currentDate = new Date();
        var selectTime = selectDate.getTime();
        var currentTime = currentDate.getTime();
        var userLen = parseInt(tmUserLen) * 60 * 1000;
        selectTime = selectTime + userLen;
        return selectTime < currentTime;
    }

    static isAfterCurrentDate(value)
    {
        var convertValue = TimeUtils.convertDateFromString(value) + ':00';
        var selectDate = TimeUtils.parserDateString(convertValue);

        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var currentDateStr = y + '-' + m + '-' + d + ' ' + '00:00:00';
        var currentDate = TimeUtils.parserDateString(currentDateStr);
        console.log('currentDate:' + currentDate)
        var selectTime = selectDate.getTime();
        var currentTime = currentDate.getTime();
        console.log('convertValue:' + convertValue + '    selectTime:' + selectTime + '        currentTime:' + currentTime)
        return selectTime > currentTime;
    }

    static formatSeconds(value)
    {
        var theTime = parseInt(value);// 秒
        var theTime1 = 0;// 分
        var theTime2 = 0;// 小时
        if (theTime >= 60)
        {
            theTime1 = parseInt(theTime / 60);
            theTime = parseInt(theTime % 60);
            if (theTime1 > 60)
            {
                theTime2 = parseInt(theTime1 / 60);
                theTime1 = parseInt(theTime1 % 60);
            }
        }
        var result = '';
        if (theTime1 > 0)
        {
            result = "" + parseInt(theTime1) + "分" + result;
        }
        if (theTime2 > 0)
        {
            result = "" + parseInt(theTime2) + "小时" + result;
        }
        return result == '60分' ? '1小时' : result;
    }

    static convertSecond(value)
    {
        var reg = /\d+/g;
        var ms = value.match(reg);
        var hour = ms[0];

        var minute = ms[1];
        if (minute != null)
        {
            var second = hour * 3600 + minute * 60;
        } else
        {
            var second = hour * 3600;
        }
        return second;
    }

    static getCurrentDate()
    {
        var nowDate = new Date().getTime();
        return nowDate;
    }

    static getCurrentDateFormatNoSecond()
    {
        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
    }

    static getBeforeDateFormat()
    {
        var date = new Date();
        date.setDate(date.getDate() - 1);//获取AddDayCount天后的日期
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    }

    static getCurrentHourMinut()
    {
        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return h + ':' + minute;
    }

    static getCurrentDateFormat()
    {
        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    }

    static convertToDate(str)
    {
        var array = str.toString().split(',');
        var year = array[0].replace('年', '');
        var month = array[1].replace('月', '');
        var day = array[2].replace('日', '');
        return month + "/" + day

    }

    static convertDayFromString(str)
    {
        var array = str.toString().split(',');
        var year = array[0].replace('年', '');
        var month = array[1].replace('月', '');
        var day = array[2].replace('日', '');
        return year + "-"
               + (month < 10 ? "0" + month : month)
               + "-"
               + (day < 10 ? "0" + day : day)
    }

    static convertDateFromString(str)
    {
        var array = str.toString().split(',');
        var year = array[0].replace('年', '');
        var month = array[1].replace('月', '');
        var day = array[2].replace('日', '');
        var hour = array[3].replace('时', '');
        var minute = array[4].replace('分', '');
        return year + "-"
               + (month < 10 ? "0" + month : month)
               + "-"
               + (day < 10 ? "0" + day : day)
               + " "
               + (hour < 10 ? "0" + hour : hour)
               + ":"
               + (minute < 10 ? "0" + minute : minute)
    }

    static convertWaterStatrDateTime(value)
    {
        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;

        var str = y + '-' + m + '-' + d + ' ' + value + ':00';
        str = moment(str).utc().format()
        return str.replace('-', '').replace('-', '').replace(' ', 'T').replace(':', '').replace(':', '')
    }

    static convertDateFromUTC(str)
    {
        str = moment(str).utc().format()
        return str.replace('-', '').replace('-', '').replace(' ', 'T').replace(':', '').replace(':', '')
    }

    static convertUTCToDate(str)
    {
        let localTime = moment.utc(str + 'Z').toDate();
        // return moment(localTime).format("YYYY-MM-DD HH:mm") + ':00';
        return moment(localTime).format("HH:mm");
    }

    static formatNextDate(utcTime)
    {
        var testDateUtc = moment.utc(utcTime);
        var localDate = moment(testDateUtc).local();
        var s = localDate.format("MM-DD HH:mm");
        return s;
    }

    /**
     * 例如:2017-06-28 10:48:46转成date类,
     * 可把- replace成/
     * @param dateString
     * @return Date
     */
    static parserDateString(dateString)
    {
        if (dateString)
        {
            let regEx = new RegExp("\\-", "gi");
            let validDateStr = dateString.replace(regEx, "/");
            let milliseconds = Date.parse(validDateStr);
            return new Date(milliseconds);
        }
    }

    // timestamp时间戳 formater时间格式
    static formatDate(timestamp, formater)
    {
        let date = new Date();
        date.setTime(parseInt(timestamp));
        formater = (formater != null) ? formater : 'yyyy-MM-dd hh:mm';
        Date.prototype.Format = function (fmt)
        {
            var o = {
                "M+": this.getMonth() + 1, //月
                "d+": this.getDate(), //日
                "h+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };

            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
            {
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ?
                    (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
            return fmt;
        }
        return date.Format(formater);
    }

    // timestamp时间戳 formater时间格式
    static formatCountDownStartDate()
    {
        let date = new Date();
        var formater = 'MM/dd/yyyy hh:mm:ss';
        Date.prototype.Format = function (fmt)
        {
            var o = {
                "M+": this.getMonth() + 1, //月
                "d+": this.getDate(), //日
                "h+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };

            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
            {
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ?
                    (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
            return fmt;
        }
        return date.Format(formater);
    }

    // timestamp时间戳 formater时间格式
    static formatCountDownEndDate(dateStr)
    {
        if (dateStr == null)
        {
            return;
        }
        var date = TimeUtils.parserDateString(dateStr);
        var formater = 'MM/dd/yyyy hh:mm:ss';
        Date.prototype.Format = function (fmt)
        {
            var o = {
                "M+": this.getMonth() + 1, //月
                "d+": this.getDate(), //日
                "h+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };

            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
            {
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ?
                    (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
            return fmt;
        }
        return date.Format(formater);
    }

    static getCurrentMonth()
    {
        let date = new Date();
        return date.getMonth() + 1;
    }

    static addHours(dateStr, number)
    {
        if (dateStr == null || dateStr == '')
        {
            return;
        }
        var fmt = 'yyyy-MM-dd hh:mm:ss';
        var date = TimeUtils.parserDateString(dateStr);

        date.setHours(date.getHours() + number);   //增加小时
        var o = {
            "M+": date.getMonth() + 1,                 //月份
            "d+": date.getDate(),                    //日
            "h+": date.getHours(),                   //小时
            "m+": date.getMinutes(),                 //分
            "s+": date.getSeconds(),                 //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds()             //毫秒
        };

        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        {
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
        return fmt;
    }

    static convertHandleTime(number)
    {
        var theTime2 = parseInt(number / 24);
        var theTime1 = parseInt(number % 24);

        var result = '';
        if (theTime1 > 0)
        {
            result = "" + parseInt(theTime1) + "小时" + result;
        }
        if (theTime2 > 0)
        {
            result = "" + parseInt(theTime2) + "天" + result;
        }
        return result;
    }

    static convertPasuingHandleTime(dateStr, timePause)
    {
        if (dateStr == null)
        {
            return;
        }
        var date = TimeUtils.parserDateString(dateStr);

        var date2 = date.setHours(date.getHours() - parseInt(timePause))//小时

        console.log(new Date(date2).Format("yyyy-MM-dd hh:mm:ss"));
        return new Date(date2).Format("yyyy-MM-dd hh:mm:ss");
    }

    static isTimeOut(limitTime)
    {
        var limitDate = TimeUtils.parserDateString(limitTime);
        var currentDate = new Date();
        var currentTime = currentDate.getTime();
        return limitDate < currentTime;
    }

    static isInPauseTimeRange(applyTimePause, timeLength)
    {
        var pauseEndTimeStr = TimeUtils.addHours(applyTimePause, timeLength);
        var pauseEndTime = TimeUtils.parserDateString(pauseEndTimeStr);
        var currentDate = new Date();
        var currentTime = currentDate.getTime();
        return currentTime < pauseEndTime;
    }
}
